package com.bnz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bbbbbbbbbbbb
 * @since 2022-10-21
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private ClassesMapper classesMapper;


    //1. 条件查询
    @Override
    public List<Student> findByWords(StudentVo vo) {

        //1.1 封装包装器对象（查询条件）
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        if (vo!=null){
            if(StringUtils.isNotBlank(vo.getSname())){
                queryWrapper.like("sname",vo.getSname());
            }
            if(StringUtils.isNotBlank(vo.getAddr())){
                queryWrapper.like("addr",vo.getAddr());
            }
        }

        //1.2 开始条件查询
        List<Student> students=baseMapper.selectList(queryWrapper);
        for (Student student : students) {
           Classes classes= classesMapper.selectById(student.getCid());
           student.setCname(classes.getCname());
        }

        return students;
    }

    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {

        IPage<Student> ipage=new Page(page,pageSize);

        IPage<Student> studentIPage=baseMapper.selectPage(ipage,null);

        return new PageResult<>(studentIPage.getTotal(), studentIPage.getRecords());
    }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVo vo) {

        IPage<Student> ipage=new Page(page,pageSize);


        QueryWrapper<Student> querywrapper=new QueryWrapper<>();
        if(vo!=null){
            if(StringUtils.isNotBlank(vo.getSname())){
                querywrapper.like("sname",vo.getSname());
            }
            if(StringUtils.isNotBlank(vo.getAddr())){
                querywrapper.like("addr",vo.getAddr());
            }
            if(vo.getCid() != null && vo.getCid() != 0){
                querywrapper.eq("cid",vo.getCid());
            }
        }


        IPage<Student> studentIPage=baseMapper.selectPage(ipage,querywrapper);


        return new PageResult<>(studentIPage.getTotal(),studentIPage.getRecords());
    }

    @Override
    public void delete(String sid) {
        baseMapper.deleteById(sid);
    }

}
